package com.zj.leetcode.medium;

import java.util.Arrays;

/**
 * @program: algorithm
 * @description: 字符串相乘
 * 43 multiply-strings
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class MultiplyStrings {
    public static void main(String[] args) {
//        String num1 = "2", num2 = "3";
//        String num1 = "9", num2 = "99";
//        String num1 = "123", num2 = "456";
//        String num1 = "0", num2 = "3";
//        String num1 = "999", num2 = "999";
        String num1 = "9133", num2 = "0";
        Solution solution = new MultiplyStrings().new Solution();
        System.out.println(solution.multiply(num1, num2));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String multiply(String num1, String num2) {
            int[] nums = new int[num1.length() + num2.length()];
            for (int i = num2.length() - 1; i >= 0; i--) {
                for (int j = num1.length() - 1; j >= 0; j--) {
                    int temp =
                            Character.getNumericValue(num2.charAt(i))
                                    * Character.getNumericValue(num1.charAt(j))
                                    + nums[i + j + 1];
                    if (temp >= 10) {
                        nums[i + j] = temp / 10 + nums[i + j];
                        nums[i + j + 1] = temp % 10;
                    } else {
                        nums[i + j + 1] = temp;
                    }

                }
            }


            StringBuilder sb = new StringBuilder();
            Arrays.stream(nums).forEach(num -> {
                sb.append(num + "");
            });
            while (sb.charAt(0) == 48 && sb.length() > 1) {
                sb.deleteCharAt(0);
            }
            return sb.toString();
        }

        public String multiply01(String num1, String num2) {
            return String.valueOf(Integer.parseInt(num1) * Integer.parseInt(num2));
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
